{extend name='app' /}
{block name='title'}项目列表{/block}

{block name="stylesheet"}
<link href="{__ADMIN__}/dist/goods/goods.css" rel="stylesheet">

{/block}

{block name="breadcrumb"}
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>项目列表</h2>
        <ol class="breadcrumb">
            <li class="breadcrumb-item">
                <a>项目管理</a>
            </li>
            <li class="breadcrumb-item active">
                <strong>项目列表</strong>
            </li>
        </ol>
    </div>
</div>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox ">
                <div class="ibox-content">
                    <form id="block-search">
                        <div class="row m-b-lg">
                            <div class="col-sm-2">
                                <div class="input-group">
                                    <input placeholder="请输入项目名称" type="text" class="form-control" name="name" value="{$search['name'] ?? ''}">
                                </div>
                            </div>
                            <div class="col-sm-2">
                                <select class="form-control m-b select-title" name="category_id">
                                    <option value="">请选择分类</option>
                                    {foreach $category as $item}
                                        <option  value="{$item->id}" {if $search['category_id']== $item->id } selected = "selected"  {/if}>{$item->name}</option>
                                    {/foreach}
                                </select>
                            </div>
                            <div class="col-sm btn-group-search">
                                <button class="btn btn-primary btn-sm" type="submit">确定</button>
                                <button class="btn btn-white btn-sm btn-reset" type="button">重置</button>
                            </div>
                        </div>
                    </form>

                    <div class="row">
                        <div class="col-sm m-b-sm">
                          <span class="float-left">
                                <button class="btn btn-primary btn-sm" type="button"
                                        onclick="window.location.href='{$Think.config.admin.app_host}/admin/project/create'"><i class="fa fa-plus"></i>&nbsp;添加
                                </button>
                            </span>

                            <span class="float-right btn-group">
                                    <button class="btn btn-white btn-sm btn-outline btn-search" type="button"><i class="fa fa-search"></i>&nbsp;搜索
                                    </button>
                            </span>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover">
                            <thead>
                            <tr>
                                <th>#</th>
                                <th>名称</th>
                                <th>缩略图</th>
                                <th>分类</th>
                                <th>积分</th>
                                <th>热门推荐</th>
                                <th>日结</th>
                                <th width="100">排序</th>
                                <th>下单等级</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {foreach $list as $index => $item}
                            <tr>
                                <td>{++$index}</td>
                                <td>{$item->name}</td>
                                <td><img src="{$item->thumb}" alt="" width="80" ></td>
                                <td>{$item->category->name}</td>
                                <td>{$item->integral}</td>
                                <td>
                                    <input type="checkbox" class="js-switch" {if $item->new_status == 1} checked {/if} onchange="change_status($(this),'{$Think.config.admin.app_host}/admin/project/renew/{$item->id}','new_status')"/>
                                </td>
                                <td>
                                    <input type="checkbox" class="js-switch" {if $item->type == 1} checked {/if} onchange="change_status($(this),'{$Think.config.admin.app_host}/admin/project/renew/{$item->id}','type')"/>
                                </td>
                                <td><input type="text" class="form-control table-input" value="{$item->sort}" onchange="change_data('{$Think.config.admin.app_host}/admin/project/renew/{$item->id}',{sort:$(this).val()})"></td>
                                <td>{$item->level->name}</td>
                                <td>
                                    {if $item->status == 0}待上线{/if}
                                    {if $item->status == 1}正常{/if}
                                    {if $item->status == 2}已关闭{/if}
                                </td>
                                <td>
                                    <button class="btn btn-info btn-xs" type="button" onclick="window.location.href='{$Think.config.admin.app_host}/admin/project/{$item->id}/edit'">
                                        <i class="fa fa-pencil"></i>&nbsp;编辑
                                    </button>
                                    <button class="btn btn-primary btn-xs" type="button" onclick="window.location.href='{$Think.config.admin.app_host}/admin/project/topic?project_id={$item->id}'">
                                        <i class="fa fa-bars"></i>&nbsp;考核题目
                                    </button>
                                    <button class="btn badge-success btn-xs" type="button" onclick="window.location.href='{$Think.config.admin.app_host}/admin/project/video?project_id={$item->id}'">
                                        <i class="fa fa-circle-o-notch"></i>&nbsp;更多视频
                                    </button>
                                </td>
                            </tr>
                            {/foreach}
                            </tbody>
                        </table>
                        <div class="float-right">
                            {$list|raw}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{/block}

{block name="script"}
<script>

    let switchery_arr = [];
    let elems = Array.prototype.slice.call(document.querySelectorAll('.js-switch'));
    elems.forEach(function (value, index) {
        let switchery = new Switchery(value, {color: '#1AB394', size: 'small'});
        switchery_arr.push(switchery);
    });


    function change_status(that, url, type) {

        // 获取当前checkbox的switchery 对象
        var index = $('.js-switch').index(that);

        let switchery = switchery_arr[index];
        var status = 0;
        if (switchery.isChecked() == true) {
            status = 1;
        }
        if(type =='status'){
            var data={'status':status};
        }else if(type =='new_status'){
            var data={'new_status':status};
        }else{
            var data={'type':status};
        }
        let response = post_request(url, data);
        if (response.status == 1) {
            show_tip('提示', response.msg, 'success');
        } else {
            show_tip('提示', response.msg, 'error');

            // 获取当前复选框的状态，并保持未change时的状态
            let status = that.prop("checked")
            if (status == false) {
                setSwitchery(switchery, true);
            } else {
                setSwitchery(switchery, false);
            }
        }
    }


    function change_data1(url, data) {
        if (!data) {
            data = {};
        }
        let response = post_request(url, data);
        if (response.status == 1) {
            show_tip('提示', response.msg, 'success');
        } else {
            show_tip('提示', response.msg, 'error');
        }
    }

</script>
{/block}
